package com.itheima.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckGroupMapper;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckGroupService;
import com.itheima.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @outhor
 * @date 2020/1/11 21:40
 */
@Service(interfaceClass = CheckGroupService.class)
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService {

    @Autowired
    private CheckGroupMapper checkGroupMapper;

    @Autowired
    private CheckItemService checkItemService;

    @Override
    public void add(CheckGroup checkGroup) {
        //插入基本信息(返回主键)-自增主键回填
        checkGroupMapper.add(checkGroup);
        Integer checkGroupId = checkGroup.getId();
        List<Integer> checkitemIds = checkGroup.getCheckitemIds();
        if (CollectionUtil.isNotEmpty(checkitemIds)) {
            List<Map> groupAnditem = new ArrayList<>();
            for (Integer checkitemId : checkitemIds) {
                Map map = new HashMap<>();
                map.put("checkGroupId", checkGroupId);
                map.put("checkitemId", checkitemId);
                groupAnditem.add(map);
            }
            checkGroupMapper.addGroupAnditem(groupAnditem);
        }
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {

        Page page = PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        List<CheckGroup> checkGroupList = checkGroupMapper.findPage(queryPageBean.getQueryString());

        return new PageResult(page.getTotal(), checkGroupList);
    }

    @Override
    public Map findByid(Integer id) {
        //通过id查询基本信息
        CheckGroup checkGroup = checkGroupMapper.findByid(id);

        //通过ID查询中间表中的CheckItem_id
        List<Integer> CheckItemIds =checkGroupMapper.findGroupAndItem(id);

        //查询所有检查项信息
        List<CheckItem> checkItemList = checkItemService.checkItemFindAll();

        //用MAP封装数据并返回
        Map map = new HashMap<>();
        map.put("checkGroup",checkGroup);
        map.put("CheckItemIds",CheckItemIds);
        map.put("checkItemList",checkItemList);
        return map;
    }

    @Override
    public void updata(CheckGroup checkGroup) {
        Integer checkGroupId = checkGroup.getId();
        List<Integer> checkitemIds = checkGroup.getCheckitemIds();
        //更新checkGroup基本信息
        checkGroupMapper.updata(checkGroup);
        //删除t_checkgroup_checkitem表中checkGroupId的数据
        checkGroupMapper.deleteGroupAndItemById(checkGroupId);
        //添加t_checkgroup_checkitem表中checkGroupId的数据
        if (CollectionUtil.isNotEmpty(checkitemIds)) {
            List<Map> groupAnditem = new ArrayList<>();
            for (Integer checkitemId : checkitemIds) {
                Map map = new HashMap<>();
                map.put("checkGroupId", checkGroupId);
                map.put("checkitemId", checkitemId);
                groupAnditem.add(map);
            }
            checkGroupMapper.addGroupAnditem(groupAnditem);
        }
    }

    @Override
    public void delete(Integer id) {
        checkGroupMapper.deleteGroupAndItemById(id);
        checkGroupMapper.delete(id);
    }

    @Override
    public List<CheckGroup> findAll() {
        return checkGroupMapper.findAll();
    }


}
